package com.decent.ejfadmin.finance.dao;

import com.decent.ejfadmin.collectcard.response.CollectCardRecordDto;
import com.decent.ejfadmin.common.bean.SupplyConsumption;
import com.decent.ejfadmin.finance.dao.provider.SupplyCardDaoProvider;
import com.decent.ejfadmin.finance.request.SupplyCardBalanceParam;
import com.decent.ejfadmin.finance.request.SupplyCardDetailParam;
import com.decent.ejfadmin.finance.response.dto.SupplyCardBalanceDto;
import com.decent.ejfadmin.finance.response.dto.SupplyCardDetailDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhangchao
 * @date 2018/7/28 8:53
 */
@Mapper
@Repository
public interface SupplyCardDao {

    /**
     * 查询销卡明细分页总数
     *
     * @param param
     * @return
     */
    @SelectProvider(type = SupplyCardDaoProvider.class, method = "querySupplyCardDetailCount")
    int querySupplyCardDetailCount(@Param("param") SupplyCardDetailParam param);

    /**
     * 查询销卡明细分页数据
     *
     * @param param
     * @return
     */
    @SelectProvider(type = SupplyCardDaoProvider.class, method = "querySupplyCardDetailPage")
    List<SupplyCardDetailDto> querySupplyCardDetailPage(@Param("param") SupplyCardDetailParam param);

    /**
     * 查询销卡余额分页总数
     *
     * @param param
     * @return
     */
    @SelectProvider(type = SupplyCardDaoProvider.class, method = "querySupplyCardBalanceCount")
    int querySupplyCardBalanceCount(@Param("param") SupplyCardBalanceParam param);

    /**
     * 查询销卡余额分页数据
     *
     * @param param
     * @return
     */
    @SelectProvider(type = SupplyCardDaoProvider.class, method = "querySupplyCardBalancePage")
    List<SupplyCardBalanceDto> querySupplyCardBalancePage(@Param("param") SupplyCardBalanceParam param);


    /**
     * 查询24小时内返点记录
     *
     * @param id 会员id
     * @return 返点明细
     */
    @Select("select id from yuhuang_user_consumption where startTime >= date_sub(now(),interval 1 DAY) and userId = #{id} and consumeType = 6")
    SupplyConsumption queryRebateRecord(@Param("id") Long id);

    /**
     * 查询没有productId的权益订单
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 订单集合
     */
    @Select("select face, area, productType, orderId from yuhuang_supply_card where starttime >= #{startTime} and starttime <= #{endTime} " +
            "AND productType > 310 and productId is null ")
    List<CollectCardRecordDto> queryNoProductIdEquityOrder(@Param("startTime") String startTime, @Param("endTime") String endTime);

    /**
     * 填充订单产品id
     *
     * @param orderId   订单id
     * @param productId 产品id
     * @return 影响条数
     */
    @Update("update yuhuang_supply_card set productId = #{productId} where orderId = #{orderId}")
    int fixProductId(@Param("orderId") String orderId, @Param("productId") String productId);
}
